 <template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.username"
                    placeholder="员工查询"
                    clearable></el-input>
          <el-input v-model="searchModel.phone"
                    placeholder="电话"
                    clearable></el-input>
          <el-button type="primary"
                     icon="el-icon-search"
                     @click="getUserList"
                     round>查询</el-button>
        </el-col>
        <el-col :span="4"
                align="right">
          <el-button @click="openEditUI(null)"
                     type="primary"
                     icon="el-icon-plus"
                     circle></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="userList"
                stripe
                style="width: 100%">
        <el-table-column label="#"
                         width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1)*pageSize +index +1 -->
            {{ (searchModel.pageNo-1) *searchModel.pageSize +scope.$index +1 }}
          </template>

        </el-table-column>
        <el-table-column prop="id"
                         label="用户ID"
                         width="180">
        </el-table-column>
        <el-table-column prop="username"
                         label="员工名 "
                         width="180">
        </el-table-column>
        <el-table-column prop="phone"
                         label="电话"
                         width="180">
        </el-table-column>
        <el-table-column prop="status"
                         label="用户状态"
                         width="180">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1">正常</el-tag>
            <el-tag v-else
                    type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="email"
                         label="电子邮件">
        </el-table-column>
        <el-table-column label="操作"
                         width="180">
          <template slot-scope="scope">
            <el-button type="primary"
                       @click="openEditUI(scope.row.id)"
                       icon="el-icon-edit"
                       size="mini"
                       circle></el-button>
            <el-button type="danger"
                       @click="deleteUser(scope.row)"
                       icon="el-icon-delete"
                       size="mini"
                       circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="searchModel.pageNo"
                   :page-sizes="[5, 10, 20, 50]"
                   :page-size="searchModel.pageSize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="total">
    </el-pagination>

    <!-- 用户信息编辑对话框 -->
    <el-dialog @close="clearForm"
               :title="title"
               :visible.sync="dialogFormVisible">
      <el-form :model="userForm"
               ref="userFormRef"
               :rules="rules">
        <el-form-item label="员工名"
                      prop="username"
                      :label-width="formLabelWidth">
          <el-input v-model="userForm.username"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="登陆密码"
                      prop="password"
                      v-if="userForm.id == null ||userForm.id == undefined"
                      :label-width="formLabelWidth">
          <el-input type="password"
                    v-model="userForm.password"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话"
                      :label-width="formLabelWidth">
          <el-input v-model="userForm.phone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户状态"
                      :label-width="formLabelWidth">
          <el-switch v-model="userForm.status"
                     :active-value="1"
                     :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="用户角色"
                      :label-width="formLabelWidth">
          <el-checkbox-group v-model="userForm.roleIdList"
                             style="width: 85%;"
                             :max="2">
            <el-checkbox v-for="role in roleList"
                         :label="role.roleId"
                         :key="role.roleId">{{role.roleDesc}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="电子邮件"
                      prop="email"
                      :label-width="formLabelWidth">
          <el-input v-model="userForm.email"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
 
 <script>
import userApi from '@/api/euserManage'
import roleApi from '@/api/roleManage'
export default {
  data () {
    return {
      roleList: [],
      formLabelWidth: '130px',
      userForm: {
        roleIdList: []
      },
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        username: '',
        phone: '',
        pageNo: 1,
        pageSize: 10
      },
      userList: [],
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入初始密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    getAllRoleList () {
      roleApi.getAllRoleList().then(respone => {
        this.roleList = respone.data
        console.log(this.roleList);
      })
    },
    deleteUser (user) {
      this.$confirm(`您确认删除用户${user.username}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userApi.deleteUserById(user.id).then(respone => {
          this.$message({
            type: 'success',
            message: respone.message
          });
          this.getUserList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    saveUser () {
      // 触发表单验证
      this.$refs.userFormRef.validate((valid) => {
        if (valid) {
          // 把提交请求给后台 
          userApi.saveUser(this.userForm).then((respone) => {
            console.log(respone);
            // 成功提示
            this.$message({
              message: respone.message,
              type: 'success'
            });
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getUserList()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    clearForm () {
      this.userForm = {
        roleIdList: []
      }
      this.$refs.userFormRef.clearValidate();

    },
    openEditUI (id) {
      if (id == null) {
        this.title = "新增用户"
      } else {
        this.title = "修改用户"
        // 根据id查询用户数据
        userApi.getUserById(id).then(respone => {
          this.userForm = respone.data
        })
      }

      this.dialogFormVisible = true
    },
    handleSizeChange (pageSize) {
      this.searchModel.pageSize = pageSize
      this.getUserList()
    },
    handleCurrentChange (pageNo) {
      this.searchModel.pageNo = pageNo
      this.getUserList()
    },
    getUserList () {
      userApi.getUserList(this.searchModel).then((respone) => {
        console.log(respone);
        this.userList = respone.data.rows
        this.total = respone.data.total
      })
    }
  },
  created () {
    this.getUserList()
    this.getAllRoleList()
  }
}
 </script>
 
 <style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 80%;
}
</style>